//
//  ViewController.m
//  JFNetYi
//
//  Created by junFung on 15/12/16.
//  Copyright © 2015年 yc. All rights reserved.
//
#define ScreenW [UIScreen mainScreen].bounds.size.width
#define ScreenH [UIScreen mainScreen].bounds.size.height

#import "ViewController.h"
#import "TopViewController.h"
#import "HotViewController.h"
#import "PhoneViewController.h"
#import "ScienceViewController.h"
#import "SocietyViewController.h"
#import "VideoViewController.h"
#import "NBAViewController.h"
#import "CarViewController.h"
#import "HealthViewController.h"

static CGFloat const titleH = 40.0;
static CGFloat const navBarH = 64.0;
static CGFloat const maxTitleScale = 1.2;

@interface ViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *titleScrollView;
@property (nonatomic, strong) UIScrollView *contentScrollView;

/** 头标题数组*/
@property (nonatomic, strong) NSMutableArray *buttonArray;
/** 记录选中的button*/
@property (nonatomic, strong) UIButton *selectedButton;

@end

@implementation ViewController

- (NSMutableArray *)buttonArray
{
    if (!_buttonArray) {
        _buttonArray = [[NSMutableArray alloc] init];
    }
    return _buttonArray;
}

- (UIScrollView *)titleScrollView
{
    if (!_titleScrollView) {
        CGRect rect = CGRectMake(0, navBarH, ScreenW, titleH);
        _titleScrollView = [[UIScrollView alloc] initWithFrame:rect];
        _titleScrollView.backgroundColor = [[UIColor lightGrayColor]colorWithAlphaComponent:0.3];
        [self.view addSubview:_titleScrollView];
        _titleScrollView.showsHorizontalScrollIndicator = NO;
    }
    return _titleScrollView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self setupContentScrollView];
}

- (void)setupContentScrollView
{
    CGFloat y = CGRectGetMaxY(self.titleScrollView.frame);
    CGRect rect = CGRectMake(0, y, ScreenW, ScreenH-navBarH);
    self.contentScrollView = [[UIScrollView alloc] initWithFrame:rect];
     self.contentScrollView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview: self.contentScrollView];
     self.contentScrollView.showsHorizontalScrollIndicator = NO;
     self.contentScrollView.pagingEnabled = YES;
     self.contentScrollView.delegate = self;
    
    [self addChildViewControllerToContentScrollView];
}

#pragma mark -- 添加子视图 --
- (void)addChildViewControllerToContentScrollView
{
    TopViewController *top = [[TopViewController alloc] init];
    HotViewController *hot = [[HotViewController alloc] init];
    PhoneViewController *phone = [[PhoneViewController alloc] init];
    ScienceViewController *science = [[ScienceViewController alloc] init];
    SocietyViewController *society = [[SocietyViewController alloc] init];
    VideoViewController *video = [[VideoViewController alloc] init];
    NBAViewController *NBA = [[NBAViewController alloc] init];
    CarViewController *car = [[CarViewController alloc] init];
    HealthViewController *health = [[HealthViewController alloc] init];
    
    [self addChildViewController:top title:@"头条"];
    [self addChildViewController:hot title:@"热点"];
    [self addChildViewController:phone title:@"手机"];
    [self addChildViewController:science title:@"科技"];
    [self addChildViewController:society title:@"社会"];
    [self addChildViewController:video title:@"视频"];
    [self addChildViewController:NBA title:@"NBA"];
    [self addChildViewController:car title:@"汽车"];
    [self addChildViewController:health title:@"健康"];

    self.contentScrollView.contentSize = CGSizeMake(self.childViewControllers.count*ScreenW, 0);
    
    [self setupTitle];
}

- (void)addChildViewController:(UIViewController *)childController title:(NSString *)title
{
    childController.title = title;
    [self addChildViewController:childController];
}

#pragma mark -- 标题 --
- (void)setupTitle
{
    NSInteger count = self.childViewControllers.count;
    
    CGFloat buttonW = 60;
    CGFloat buttonH = titleH;
    for (int i = 0 ; i < count; i ++) {
        UIViewController *vc = self.childViewControllers[i];
    
        CGFloat buttonX = i*buttonW;
        CGRect buttonFrame = CGRectMake(buttonX, 0, buttonW, buttonH);
        UIButton *btn = [[UIButton alloc] initWithFrame:buttonFrame];
        [btn setTitle:[vc title] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:14.0];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(titleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        btn.tag = i;
        
        [self.buttonArray addObject:btn];
        //默认选中第一个
        if (i == 0) { [self titleBtnClick:btn]; }
        
        [self.titleScrollView addSubview:btn];
    }
    self.titleScrollView.contentSize = CGSizeMake(buttonW*count, 0);
}

#pragma mark -- 点击标题 --
- (void)titleBtnClick:(UIButton *)sender
{
    //标题
    //上一个button变暗
    [self.selectedButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.selectedButton.transform = CGAffineTransformIdentity;
    //当前选中的button
    [sender setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    sender.transform = CGAffineTransformMakeScale(maxTitleScale, maxTitleScale);
    
    self.selectedButton = sender;
    
    //翻页
    NSInteger i = sender.tag;
    [self setupChildViewToContentScrollView:i];
    self.contentScrollView.contentOffset = CGPointMake(i*ScreenW, 0);
    
    [self setupTitleBtnToCenter:sender];
}

#pragma mark -- 让标题居中 --
- (void)setupTitleBtnToCenter:(UIButton *)sender
{
    CGFloat offset = sender.center.x - ScreenW * 0.5;
    
//    NSLog(@"%f",offset);
    
    if (offset < 0.0) {
        offset = 0.0;
    }
    
    CGFloat maxOffset = self.titleScrollView.contentSize.width - ScreenW;
    if (offset > maxOffset) {
        offset = maxOffset;
    }
    [self.titleScrollView setContentOffset:CGPointMake(offset, 0) animated:YES];
}

#pragma mark --将每个控制器的视图添加到contentScrollView --
- (void)setupChildViewToContentScrollView:(NSInteger)index
{
    CGFloat viewX = index*ScreenW;
    CGFloat viewH = ScreenH - self.contentScrollView.frame.origin.y;
    
    UIViewController *vc = self.childViewControllers[index];
    
    //如果当前这个视图已经添加到别的视图上面就不在添加，直接返回
    if (vc.view.superview) {
        NSLog(@"------- 该视图已经添加过了");
        return;
    }
    vc.view.frame = CGRectMake(viewX, 0, ScreenW, viewH);
    [self.contentScrollView addSubview:vc.view];
}

#pragma mark --- scrollView Delegate ---
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger i = self.contentScrollView.contentOffset.x/ScreenW;
    [self setupTitleBtnToCenter:self.buttonArray[i]];
    [self setupChildViewToContentScrollView:i];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offsetX = self.contentScrollView.contentOffset.x;
    
    NSInteger leftButtonIndex = offsetX/ScreenW;
    NSInteger rightButtonIndex = leftButtonIndex + 1;
//    NSLog(@"leftButtonIndex =%zd,rightButtonIndex = %zd",leftButtonIndex,rightButtonIndex);
    
    UIButton *leftButton = self.buttonArray[leftButtonIndex];
    
    UIButton *rightButton = nil;
    if (rightButtonIndex < self.buttonArray.count) { //防止数组越界
        rightButton =  self.buttonArray[rightButtonIndex];
    }
    
    if (offsetX > 0.0 && offsetX < ScreenW*self.buttonArray.count) {
        
        CGFloat scaleR = offsetX / ScreenW - leftButtonIndex;
        
        CGFloat scaleL = 1 - scaleR;
        
        CGFloat transScale = maxTitleScale - 1;
        leftButton.transform = CGAffineTransformMakeScale(scaleL * transScale + 1, scaleL * transScale + 1);
        
        rightButton.transform = CGAffineTransformMakeScale(scaleR * transScale + 1, scaleR * transScale + 1);
        
        UIColor *rightColor = [UIColor colorWithRed:scaleR green:0 blue:0 alpha:1];
        UIColor *leftColor = [UIColor colorWithRed:scaleL green:0 blue:0 alpha:1];
        
        [leftButton setTitleColor:leftColor forState:UIControlStateNormal];
        [rightButton setTitleColor:rightColor forState:UIControlStateNormal];
        
        
    }
}
@end
